package com.kkb.service.file.impl;

import com.kkb.bean.FileTable;
import com.kkb.bean.FileTagsTable;
import com.kkb.bean.TagsTable;
import com.kkb.dao.FileTableMapper;
import com.kkb.dao.FileTagsTableMapper;
import com.kkb.dao.TagsTableMapper;
import com.kkb.service.file.FileTableService;
import com.kkb.utils.util.MassageReturnUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Author 何世鹏
 * @Date 2021/4/12 10:56
 */
@Service
public class FileTableServiceImpl implements FileTableService {

    @Autowired
    private FileTableMapper fileTableMapper;

    @Autowired
    private TagsTableMapper tagsTableMapper;

    @Autowired
    private FileTagsTableMapper fileTagsTableMapper;


    /**
     * @param req   请求
     * @param resp  响应
     * @param state 状态信息条件查询
     * @return json格式的信息
     */
    @Override
    public String getList(HttpServletRequest req, HttpServletResponse resp, Integer state, Integer type) {
        //从请求中获取session
        //HttpSession session = req.getSession();
        //获取用户id
        Integer id = 1;//(Integer) session.getAttribute("id");
        if (id != null){
            //此处说明登录了
            FileTable fileTable = new FileTable();
            //设置用户ID
            fileTable.setUserId(id);
            if (state != null){
                //按照state条件查询
                fileTable.setState(state);
            }
            fileTable.setFileType(type);
            fileTable.setDelState(0);
            //查询FileTable,此处全部返给前端，前端分页
            List<FileTable> select = fileTableMapper.select(fileTable);
            for (FileTable table : select) {
                //给前端返回绝对地址
                String realPath = req.getServletContext().getRealPath("/upload/file") + "/";
                //文档设置绝对地址
                String filePath = table.getFilePath();
                table.setFilePath(realPath+filePath);
                //封面设置绝对地址
                String fileCoverPath = table.getFileCoverPath();
                table.setFileCoverPath(realPath+fileCoverPath);
            }
            return MassageReturnUtils.toJson("0", "查询成功", select);
        }
        return MassageReturnUtils.toJson("-1", "未登录", null);
    }

    /**
     * 添加文档
     *
     * @param fileTable 文档信息
     * @return 成功true, 失败false
     */
    @Override
    public Boolean addList(HttpServletRequest req, HttpServletResponse resp, FileTable fileTable) {
        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");

        if (id != null){
            fileTable.setNumber(0);
            fileTable.setLookNumber(0);
            fileTable.setDownloadNumber(0);
            fileTable.setUpTime(new Date());
            fileTable.setState(0);
            fileTable.setDelState(0);
            fileTable.setUserId(id);
            FileTable f = fileTable;
            int insert = fileTableMapper.insert(fileTable);
            if (insert > 0){
                Integer fileId = fileTable.getFileId();
                List<TagsTable> tags = fileTable.getTags();
                if (tags != null){
                    for (TagsTable tag : tags) {
                        TagsTable tagsTable = tagsTableMapper.selectOne(tag);
                        FileTagsTable fileTagsTable = new FileTagsTable(fileId, tagsTable.getTargsId());
                        List<FileTagsTable> select = fileTagsTableMapper.select(fileTagsTable);
                        if (select == null) {
                            fileTagsTableMapper.insert(fileTagsTable);
                        }
                    }
                }
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

    /**
     * 获取文件下载地址
     *
     * @param req  请求
     * @param resp 响应
     * @param fileId 文件ID
     * @return 文件路径
     */
    @Override
    public String getFilePath(HttpServletRequest req, HttpServletResponse resp, Integer fileId) {
        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");
        FileTable fileTable = fileTableMapper.selectOne(new FileTable(fileId));
        if (fileTable != null) {
            return fileTable.getFilePath();
        }else {
            return null;
        }
    }

    /**
     * 下架or删除文件
     *
     * @param fileId 删除的文件ID
     */
    @Override
    public void delFile(HttpServletRequest req, HttpServletResponse resp,Integer fileId) {

        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");
        FileTable fileTable1 = new FileTable();
        fileTable1.setFileId(fileId);
        FileTable fileTable = fileTableMapper.selectOne(fileTable1);
        if (fileTable != null && id != null && fileTable.getUserId().equals(id)){
            fileTable.setDelState(1);
            fileTableMapper.updateByPrimaryKey(fileTable);
            //删除对应的中间表关系
            FileTagsTable fileTagsTable = new FileTagsTable();
            fileTagsTable.setFileId(fileId);
            fileTagsTableMapper.delete(fileTagsTable);
        }
    }

    /**
     * 修改回显数据专用接口
     *
     * @param fileId 文档ID
     * @return 文档对象
     */
    @Override
    public FileTable selectById(HttpServletRequest req, HttpServletResponse resp,Integer fileId) {
        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");
        if (id != null){
            FileTable fileTable1 = new FileTable(fileId);
            fileTable1.setUserId(id);
            FileTable fileTable = fileTableMapper.selectOne(fileTable1);
            String realPath = req.getServletContext().getRealPath("/upload/file") + "/";
            if (fileTable != null) {
                //文档设置绝对地址
                String filePath = fileTable.getFilePath();
                fileTable.setFilePath(realPath+filePath);
                //封面设置绝对地址
                String fileCoverPath = fileTable.getFileCoverPath();
                fileTable.setFileCoverPath(realPath+fileCoverPath);
            }
            return fileTable;
        }else {
            //没登陆
            return null;
        }

    }

    /**
     * 修改文件信息
     *
     * @param req       请求
     * @param resp      响应
     * @param fileTable 修改的文件信息
     * @return 成功true, 失败false
     */
    @Override
    public Boolean updateFile(HttpServletRequest req, HttpServletResponse resp, FileTable fileTable) {
        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");

        if (id != null){
            int insert = fileTableMapper.updateByPrimaryKey(fileTable);
            if (insert > 0){
                Integer fileId = fileTable.getFileId();
                List<TagsTable> tags = fileTable.getTags();
                if (tags != null){
                    for (TagsTable tag : tags) {
                        TagsTable tagsTable = tagsTableMapper.selectOne(tag);
                        FileTagsTable fileTagsTable = new FileTagsTable(fileId, tagsTable.getTargsId());
                        List<FileTagsTable> select = fileTagsTableMapper.select(fileTagsTable);
                        if (select == null) {
                            fileTagsTableMapper.insert(fileTagsTable);
                        }
                    }
                }
                return true;
            }else {
                return false;
            }
        }
        return false;
    }


}
